package com.seio.edc.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.model.system.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;

/**
 * 系统的基础数据接口
 */
@Controller
@RequestMapping(value = "/baseApi")
public class BaseAPIController {

    private static final int PAGE_SIZE = 10;

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    /**
     * 显示登录画面
     */
    @RequestMapping(value = "findCommonCustomers.shtml")
    @ResponseBody
    public Map<String, Object> userNameVali(HttpServletRequest request,
                                            HttpSession session, Integer currentPage, ModelMap modelMap,
                                            String customerName) {
        int pageIndex = Integer.parseInt(request.getParameter("page.currentPage"));
        int pagesize = Integer.parseInt(request.getParameter("page.pageSize"));
        Map<String, Object> map = new HashMap<String, Object>();
        // 当前页数
        Page page = new Page();
        page.setCurrentPage(pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(pagesize == 0 ? PAGE_SIZE : pagesize);
        // 获取数据为当前登录用户添加的数据
        Login login = (Login) session.getAttribute("sessionLogin");
        PageList<CustomerInfo> pageList = novaCustomerInfoService
                .findCustomerInfoPageListByRole(login, 0, customerName, 0, page,
                        "", "desc");
        map.put("page", page);
        // 数据列表
        map.put("custList", pageList.getDatas());
        // 获取表单值并封装到实体内
        return map;
    }
}
